package com.zxbit.flash.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.otaliastudios.cameraview.AspectRatio;
import com.otaliastudios.cameraview.CameraUtils;
import com.zxbit.flash.R;
import com.zxbit.flash.utils.FileUtils;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

import crossoverone.statuslib.StatusUtil;


public class PicturePreviewActivity extends Activity implements View.OnClickListener {
    private LinearLayout ll_bottom;
    private TextView tv_confirm;
    private TextView tv_cancel;
    private static SoftReference<byte[]> image;

    public static byte[] getImage() {
        return image != null ? image.get() : null;
    }

    public static void setImage(@Nullable byte[] im) {
        image = im != null ? new SoftReference<>(im) : null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusUtil.setUseStatusBarColor(this, Color.parseColor("#00000000"));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);

        setContentView(R.layout.activity_picture_preview);
        ll_bottom = findViewById(R.id.ll_bottom);
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_confirm.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        final ImageView imageView = findViewById(R.id.image);


        final long delay = getIntent().getLongExtra("delay", 0);
        final int nativeWidth = getIntent().getIntExtra("nativeWidth", 0);
        final int nativeHeight = getIntent().getIntExtra("nativeHeight", 0);
        byte[] b = image == null ? null : image.get();
        if (b == null) {
            finish();
            return;
        }

        CameraUtils.decodeBitmap(b, 1000, 1000, new CameraUtils.BitmapCallback() {
            @Override
            public void onBitmapReady(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);

                AspectRatio nativeRatio = AspectRatio.of(nativeWidth, nativeHeight);
                ll_bottom.setVisibility(View.VISIBLE);


            }
        });

    }

    private static float getApproximateFileMegabytes(Bitmap bitmap) {
        return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024 / 1024;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                String filename = FileUtils.byte2File(image.get());
                Intent intent = new Intent();
                intent.putExtra("filename", filename);
                setResult(0x01, intent);
                finish();
                break;
            case R.id.tv_cancel:
                setResult(0x02);
                finish();
                break;
            default:
                break;
        }

    }
}